"""
超级管理员相关Pydantic模式
创建时间：2025-09-17
作者：开发团队
目的：定义超级管理员功能相关的数据验证和序列化模式
"""

from pydantic import BaseModel, Field, field_validator
import json
from datetime import datetime


# 管理员管理相关模式
class AdminBase(BaseModel):
    """管理员基础模式"""
    username: str = Field(..., min_length=3, max_length=50, description="用户名")
    name: str = Field(..., min_length=1, max_length=100, description="姓名")
    phone: str | None = Field(None, description="手机号")
    campus_id: int | None = Field(None, description="院区ID")
    status: str = Field("active", pattern="^(active|disabled)$", description="状态")


class AdminCreate(AdminBase):
    """管理员创建模式"""
    password: str = Field(..., min_length=6, max_length=50, description="密码")

    @field_validator('username')
    @classmethod
    def username_alphanumeric(cls, v: str) -> str:
        assert v.isalnum() or v.replace('_', '').isalnum(), '用户名只能包含字母、数字和下划线'
        return v


class AdminUpdate(BaseModel):
    """管理员更新模式"""
    name: str | None = Field(None, min_length=1, max_length=100, description="姓名")
    phone: str | None = Field(None, description="手机号")
    campus_id: int | None = Field(None, description="院区ID")
    status: str | None = Field(None, pattern="^(active|disabled)$", description="状态")


class AdminResponse(AdminBase):
    """管理员响应模式"""
    id: int
    created_at: datetime
    last_login: datetime | None = None

    class Config:
        from_attributes = True


# 院区管理相关模式
class CampusBase(BaseModel):
    """院区基础模式"""
    name: str = Field(..., min_length=1, max_length=100, description="院区名称")
    address: str | None = Field(None, max_length=200, description="地址")
    contact_person: str | None = Field(None, max_length=50, description="联系人")
    contact_phone: str | None = Field(None, description="联系电话")


class CampusCreate(CampusBase):
    """院区创建模式"""
    pass


class CampusUpdate(BaseModel):
    """院区更新模式"""
    name: str | None = Field(None, min_length=1, max_length=100, description="院区名称")
    address: str | None = Field(None, max_length=200, description="地址")
    contact_person: str | None = Field(None, max_length=50, description="联系人")
    contact_phone: str | None = Field(None, description="联系电话")


class CampusResponse(CampusBase):
    """院区响应模式"""
    id: int
    created_at: datetime
    updated_at: datetime

    class Config:
        from_attributes = True


# 系统监控相关模式
class SystemStatusResponse(BaseModel):
    """系统状态响应模式"""
    server: dict[str, object]
    database: dict[str, object]
    services: list[object]

    class Config:
        from_attributes = True


# 数据包生成相关模式
class DataPackageCreate(BaseModel):
    """数据包创建模式"""
    content_types: list[str] = Field(..., description="内容类型列表")
    encryption: bool = Field(True, description="是否加密")


class DataPackageResponse(BaseModel):
    """数据包响应模式"""
    id: int
    content_types: list[str]
    file_size: int
    encryption_status: str
    download_count: int
    created_at: datetime
    download_url: str | None = None

    @field_validator('content_types', mode='before')
    @classmethod
    def parse_content_types(cls, v: str | list[str]) -> list[str]:
        if isinstance(v, str):
            try:
                return json.loads(v)
            except:
                return []
        return v

    class Config:
        from_attributes = True